<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use think\Model;

class ArticleProxy extends Model
{

    /**
     * @param int $mid
     * @param int $mc_id
     * @param string $out
     * @return bool
     */
    public function check($mid=0,$mc_id=0,&$out=''){
        if(!$mid || !$mc_id){
            $out = '缺少参数';
            return false;
        }
        $res = $this
            ->where('aid','eq',$mid)
            ->where('mc_id','eq',$mc_id)
            ->count();
        return $res?true:false;
    }

    /**
     * 申请代理
     * @param $mid
     * @param $mc_id
     * @param string $name
     * @param string $tel
     * @param string $out
     * @return bool|string
     */
    public function addNew($mid,$mc_id,$name='',$tel='',&$out=''){
        $count1 = $this
            ->where('status','eq',2)
            ->where('mc_id','eq',$mc_id)
            ->count();
        if($count1){
            $out = '该商户已有代理了';
            return false;
        }
        $count = $this
            ->where('mid','eq',$mid)
            ->where('mc_id','eq',$mc_id)
            ->count();
        if($count){
            $out = '您已经是或正在申请该商户的代理';
            return false;
        }
        $validate = new \app\index\validate\ArticleProxy();
        $data = [
            'mid'=>$mid,
            'mc_id'=>$mc_id,
            'name'=>$name,
            'create_time'=>time(),
            'tel'=>$tel
        ];
        if(!$validate->check($data)){
            $out = $validate->getError();
            return false;
        }
        if(!$this->insert($data)){
            $out = '申请失败';
            return false;
        }
        return $this->getLastInsID();
    }
}